/**
 * 
 */
package com.zznode.client.wlanpolling.other;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.zznode.client.wlanpolling.R;
import com.zznode.client.wlanpolling.model.WebSite;

/**
 * @author Pink
 *
 */
public class WebSiteListAdapter extends BaseAdapter {
	private List<WebSite> webSites;
	private Context context;

	public WebSiteListAdapter(List<WebSite> websites, Context context) {
		this.webSites = websites;
		this.context = context;
	}
	
	@Override
	public int getCount() {
		return webSites.size();
	}

	@Override
	public Object getItem(int position) {
		return webSites.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		
        if(view == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.websitelist_item, null);
        }
        
        WebSite webSite = webSites.get(position);
        TextView tvName = (TextView)view.findViewById(R.id.websitename);
        tvName.setText(webSite.getName());
        TextView tvUrl = (TextView)view.findViewById(R.id.weburl);
        tvUrl.setText(webSite.getUrl());
        
        CheckBox cbBox = (CheckBox)view.findViewById(R.id.websitecheckbox);
        cbBox.setChecked(webSite.isSelected());
        cbBox.setTag(position);
        cbBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				int position = (Integer)buttonView.getTag();
				if(position >= webSites.size())
					return;
				
				if(isChecked) {
					(webSites.get(position)).setSelected(true);
				}else {
					(webSites.get(position)).setSelected(false);
				}
			}
		});
        
		return view;
	}

	public List<WebSite> getWebSites() {
		return webSites;
	}

	public void setWebSites(List<WebSite> webSites) {
		this.webSites = webSites;
	}
}
